var NewPerson = {
	newperson: null,
	InitBeijing: function(aTo) {
	    var listFn=function(aTo){
		$ajax({
			url: "/newperson/bj",
			data:{
			   category: BROADCASTTYPE.TYPE_NEWPERSON,
			   aTo:0
			},
			success: function(res) {
				if(res.code)
					return $alert(res.codemsg);
				var items = res.result.results;
				SetLS("plist0", items);
				var html = "";
				if(!items||items.length==0||items==undefined){
                       html += '<div class="">'+
                                  '<div class="tx-c umt_p60">'+(CurrentUser.category==30?"亲，当前城市没有老外求助信息！":"There is no help information at this moment!")+'</div>'+
                                '</div>';    
                    }
				for (var i=0; i<items.length; ++i) {
					var ask=items[i].present;
					var time=items[i].create;
					if(!ask||ask.length==0||ask==undefined){
                       ask=CurrentUser.category==30?"无":"No";
                    }
				html += '<div class="ub uw99 hide ub up_list3 detailEvt" ontouchstart="zy_touch()" onclick="NewPerson.detail(\''+ i +'\',\''+0+'\')">'+
                        '     <div class="ub ub-ver border-newperson-detail1 umt_p5 uml_p5 umb_1 ">'+
                        '         <div class="ub ub-ver col_r">My Request:</div>'+
                        '         <div class="ub ub-ver umt_p3 wb_c">'+ask+'</div>'+
                        '        <div class="ub  umt_1 umb_p3">'+
                        '           <div class="ub-img uw1p5 uh1p2 im_icon_phone_no "></div>'+
                        '          <div class="ub ub-ver">'+items[i].phone+'</div>'+
                        '          <div class="ub-img uw1p2 uh1p2 im_set_icon9 uml_3"></div>'+
                        '         <div class="ub ub-ver">'+ (items[i].aFrom?items[i].aFrom.substring(0,6):"") +'</div>'+
                        '      </div>'+
                        '     </div>'+
                        '     <div class="ub ub-ver ub_right  ub-f1">'+
                        '      <div class="uw6 uh6 umt_pf8 ub_img  ub-f1 ub-ae ub-pe" style="background-image:url('+ Core.id2url(items[i].photo) +')"></div>'+
                        '      <div class="ub ub-ver uw6 tx-c umt_pf8 ub-f1">'+ items[i].name +'</div>'+
                        '      <div class="ufp6 ut-s uw6 gray2 uml_6p5  umt_11 pso_abs  ub-f1 ub-ae ub-pe">'+ (new Date(time)).pattern('yyyy-MM-dd')+'</div>'+
                        '  </div>'+
                        '</div>'+
                        '</div>';
				}
				$("#beiJing").html(html);
			}
		});
	   }
		listFn(aTo);
	},
	InitShanghai: function(aTo) {
        $ajax({
            url: "/newperson/sh",
            data:{
               category: BROADCASTTYPE.TYPE_NEWPERSON,
               aTo:1
            },
            success: function(res) {
                if(res.code)
                    return $alert(res.codemsg);

                var items = res.result.results;
                SetLS("plist1", items);
                var html = "";
                if(!items||items.length==0||items==undefined){
                       html += '<div>'+
                                  '<div class="tx-c umt_p60">'+(CurrentUser.category==30?"亲，当前城市没有老外求助信息！":"There is no help information at this moment!")+'</div>'+
                                '</div>';    
                    }
                for (var i=0; i<items.length; ++i) {
                    var ask=items[i].present;
                    var time=items[i].create;
                    if(!ask||ask.length==0||ask==undefined){
                       ask=CurrentUser.category==30?"无":"No";
                    }
                    html += '<div class="ub uw99 hide ub up_list3 detailEvt" ontouchstart="zy_touch()" onclick="NewPerson.detail(\''+ i +'\',\''+1+'\')">'+
                            '     <div class="ub ub-ver border-newperson-detail1 umt_p5 uml_p5 umb_1 ">'+
                            '         <div class="ub ub-ver col_r">My Request:</div>'+
                            '         <div class="ub ub-ver umt_p3 wb_c">'+ask+'</div>'+
                            '        <div class="ub  umt_1 umb_p3">'+
                            '           <div class="ub-img uw1p5 uh1p2 im_icon_phone_no "></div>'+
                            '          <div class="ub ub-ver">'+items[i].phone+'</div>'+
                            '          <div class="ub-img uw1p2 uh1p2 im_set_icon9 uml_3"></div>'+
                            '         <div class="ub ub-ver">'+ (items[i].aFrom?items[i].aFrom.substring(0,6):"") +'</div>'+
                            '      </div>'+
                            '     </div>'+
                            '     <div class="ub ub-ver ub_right  ub-f1">'+
                            '      <div class="uw6 uh6 umt_pf8 ub_img  ub-f1 ub-ae ub-pe" style="background-image:url('+ Core.id2url(items[i].photo) +')"></div>'+
                            '      <div class="ub ub-ver uw6 tx-c umt_pf8 ub-f1">'+ items[i].name +'</div>'+
                            '      <div class="ufp6 ut-s uw6 gray2 uml_6p5  umt_11 pso_abs  ub-f1 ub-ae ub-pe">'+ (new Date(time)).pattern('yyyy-MM-dd')+'</div>'+
                            '  </div>'+
                            '</div>'+
                            '</div>';                      
                }
                $("#shangHai").html(html);
            }
        });
    },
    InitGuangzhou: function(aTo) {
        var listFn=function(aTo){
        $ajax({
            url: "/newperson/gz",
            data:{
               category: BROADCASTTYPE.TYPE_NEWPERSON,
               aTo:2
            },
            success: function(res) {
                if(res.code)
                    return $alert(res.codemsg);
                var items = res.result.results;
                SetLS("plist2", items);
                var html = "";
                if(!items||items.length==0||items==undefined){
                       html += '<div class="">'+
                                  '<div class="tx-c umt_p60">'+(CurrentUser.category==30?"亲，当前城市没有老外求助信息！":"There is no help information at this moment!")+'</div>'+
                                '</div>';    
                    }
                for (var i=0; i<items.length; ++i) {
                    var ask=items[i].present;
                    var time=items[i].create;
                    if(!ask||ask.length==0||ask==undefined){
                       ask=CurrentUser.category==30?"无":"No";
                    }
                    html += '<div class="ub uw99 hide ub up_list3 detailEvt" ontouchstart="zy_touch()" onclick="NewPerson.detail(\''+ i +'\',\''+2+'\')">'+
                            '     <div class="ub ub-ver border-newperson-detail1 umt_p5 uml_p5 umb_1 ">'+
                            '         <div class="ub ub-ver col_r">My Request:</div>'+
                            '         <div class="ub ub-ver umt_p3 wb_c">'+ask+'</div>'+
                            '        <div class="ub  umt_1 umb_p3">'+
                            '           <div class="ub-img uw1p5 uh1p2 im_icon_phone_no "></div>'+
                            '          <div class="ub ub-ver">'+items[i].phone+'</div>'+
                            '          <div class="ub-img uw1p2 uh1p2 im_set_icon9 uml_3"></div>'+
                            '         <div class="ub ub-ver">'+ (items[i].aFrom?items[i].aFrom.substring(0,6):"") +'</div>'+
                            '      </div>'+
                            '     </div>'+
                            '     <div class="ub ub-ver ub_right  ub-f1">'+
                            '      <div class="uw6 uh6 umt_pf8 ub_img  ub-f1 ub-ae ub-pe" style="background-image:url('+ Core.id2url(items[i].photo) +')"></div>'+
                            '      <div class="ub ub-ver uw6 tx-c umt_pf8 ub-f1">'+ items[i].name +'</div>'+
                            '      <div class="ufp6 ut-s uw6 gray2 uml_6p5  umt_11 pso_abs  ub-f1 ub-ae ub-pe">'+ (new Date(time)).pattern('yyyy-MM-dd')+'</div>'+
                            '  </div>'+
                            '</div>'+
                            '</div>';                  
                }
                $("#guangZhou").html(html);
            }
        });
       }
        listFn(aTo);
    },
    InitShenzhen: function(aTo) {
        var listFn=function(aTo){
        $ajax({
            url: "/newperson/sz",
            data:{
               category: BROADCASTTYPE.TYPE_NEWPERSON,
               aTo:3
            },
            success: function(res) {
                if(res.code)
                    return $alert(res.codemsg);

                var items = res.result.results;
                SetLS("plist3", items);
                var html = "";
                if(!items||items.length==0||items==undefined){
                       html += '<div class="">'+
                                  '<div class="tx-c umt_p60">'+(CurrentUser.category==30?"亲，当前城市没有老外求助信息！":"There is no help information at this moment!")+'</div>'+
                                '</div>';    
                    }
                for (var i=0; i<items.length; ++i) {
                    var ask=items[i].present;
                    var time=items[i].create;
                    if(!ask||ask.length==0||ask==undefined){
                       ask=CurrentUser.category==30?"无":"No";
                    }
                    html += '<div class="ub uw99 hide ub up_list3 detailEvt" ontouchstart="zy_touch()" onclick="NewPerson.detail(\''+ i +'\',\''+3+'\')">'+
                            '     <div class="ub ub-ver border-newperson-detail1 umt_p5 uml_p5 umb_1 ">'+
                            '         <div class="ub ub-ver col_r">My Request:</div>'+
                            '         <div class="ub ub-ver umt_p3 wb_c">'+ask+'</div>'+
                            '        <div class="ub  umt_1 umb_p3">'+
                            '           <div class="ub-img uw1p5 uh1p2 im_icon_phone_no "></div>'+
                            '          <div class="ub ub-ver">'+items[i].phone+'</div>'+
                            '          <div class="ub-img uw1p2 uh1p2 im_set_icon9 uml_3"></div>'+
                            '         <div class="ub ub-ver">'+ (items[i].aFrom?items[i].aFrom.substring(0,6):"") +'</div>'+
                            '      </div>'+
                            '     </div>'+
                            '     <div class="ub ub-ver ub_right  ub-f1">'+
                            '      <div class="uw6 uh6 umt_pf8 ub_img  ub-f1 ub-ae ub-pe" style="background-image:url('+ Core.id2url(items[i].photo) +')"></div>'+
                            '      <div class="ub ub-ver uw6 tx-c umt_pf8 ub-f1">'+ items[i].name +'</div>'+
                            '      <div class="ufp6 ut-s uw6 gray2 uml_6p5  umt_11 pso_abs  ub-f1 ub-ae ub-pe">'+ (new Date(time)).pattern('yyyy-MM-dd')+'</div>'+
                            '  </div>'+
                            '</div>'+
                            '</div>'; 
                }
                $("#shenZhen").html(html);
            }
        });
       }
        listFn(aTo);
    },
    InitOthers: function(aTo) {
        var listFn=function(aTo){
        $ajax({
            url: "/newperson/qt",
            data:{
               category: BROADCASTTYPE.TYPE_NEWPERSON,
               aTo:4
            },
            success: function(res) {
                if(res.code)
                    return $alert(res.codemsg);
                var items = res.result.results;
                SetLS("plist4", items);
                var html = "";
                if(!items||items.length==0||items==undefined){
                       html += '<div class="">'+
                                  '<div class="tx-c umt_p60">'+(CurrentUser.category==30?"亲，当前城市没有老外求助信息！":"There is no help information at this moment!")+'</div>'+
                                '</div>';    
                    }
                for (var i=0; i<items.length; ++i) {
                    var ask=items[i].present;
                    var time=items[i].create;
                    if(!ask||ask.length==0||ask==undefined){
                       ask=CurrentUser.category==30?"无":"No";
                    }
                    html += '<div class="ub uw99 hide ub up_list3 detailEvt" ontouchstart="zy_touch()" onclick="NewPerson.detail(\''+ i +'\',\''+4+'\')">'+
                            '     <div class="ub ub-ver border-newperson-detail1 umt_p5 uml_p5 umb_1 ">'+
                            '         <div class="ub ub-ver col_r">My Request:</div>'+
                            '         <div class="ub ub-ver umt_p3 wb_c">'+ask+'</div>'+
                            '        <div class="ub  umt_1 umb_p3">'+
                            '           <div class="ub-img uw1p5 uh1p2 im_icon_phone_no "></div>'+
                            '          <div class="ub ub-ver">'+items[i].phone+'</div>'+
                            '          <div class="ub-img uw1p2 uh1p2 im_set_icon9 uml_3"></div>'+
                            '         <div class="ub ub-ver">'+ (items[i].aFrom?items[i].aFrom.substring(0,6):"") +'</div>'+
                            '      </div>'+
                            '     </div>'+
                            '     <div class="ub ub-ver ub_right  ub-f1">'+
                            '      <div class="uw6 uh6 umt_pf8 ub_img  ub-f1 ub-ae ub-pe" style="background-image:url('+ Core.id2url(items[i].photo) +')"></div>'+
                            '      <div class="ub ub-ver uw6 tx-c umt_pf8 ub-f1">'+ items[i].name +'</div>'+
                            '      <div class="ufp6 ut-s uw6 gray2 uml_6p5  umt_11 pso_abs  ub-f1 ub-ae ub-pe">'+ (new Date(time)).pattern('yyyy-MM-dd')+'</div>'+
                            '  </div>'+
                            '</div>'+
                            '</div>';                    
                }
                $("#others").html(html);
            }
        });
       }
        listFn(aTo);
    },
	detail: function(i,index) {
	    var item = "";
	    if(index == "0"){
            item = GetLS("plist0")[i];
        }
        if(index == "1"){
            item = GetLS("plist1")[i];
        }
        if(index == "2"){
            item = GetLS("plist2")[i];
        }
        if(index == "3"){
            item = GetLS("plist3")[i];
        }
        if(index == "4"){
            item = GetLS("plist4")[i];
        }
		SetLS("newperson_detail",item);
		OpenNewWin("newperson_detail");
	},
	detailInit: function() {
		var person = GetLS("newperson_detail");
		var photo = (person.photo&&person.photo[0])?person.photo[0]:"../img/foreign.png";
		$("#photo").css("background-image", "url("+ photo +")");
		$("#name").text(person.name);
		$("#uname").text(person.name);
		$("#phone").text(person.phone);
		$("#view").text(person.view);
		if(person.view==undefined){
		  $("#view").text(0);
		}
		var wu=CurrentUser.category==30?"无":"No";
		$("#aFrom").text((person.aFrom?person.aFrom:wu));
		$("#aTo").text(person.aTo);
            if(person.aTo==0){
               $("#aTo").text("BeiJing");
            }
            if(person.aTo==1){
               $("#aTo").text("ShangHai");
            }
            if(person.aTo==2){
               $("#aTo").text("GuangZhou");
            }
            if(person.aTo==3){
               $("#aTo").text("ShenZhens");
            }
            if(person.aTo==4){
               $("#aTo").text(person.aToOther);
            }
		//$("#aTo").text(person.aTo?person.aTo:wu);
		$("#present").text(person.present?person.present:wu);
		$("#create").text(new Date(person.create).pattern("yyyy-MM-dd HH:mm:ss"));
		 $ajax({
                url: "/newperson/views",
                data: person,
                success: function(res) {
                    var ml=CurrentUser.category==30?"ch/":"en/";
                    PCall("../"+ml+"newperson", "CCall('NewPerson.InitBeijing();')");
                    PCall("../"+ml+"newperson", "CCall('NewPerson.InitShanghai();')");
                    PCall("../"+ml+"newperson", "CCall('NewPerson.InitGuangzhou();')");
                    PCall("../"+ml+"newperson", "CCall('NewPerson.InitShenzhen();')");
                    PCall("../"+ml+"newperson", "CCall('NewPerson.InitOthers();')"); 
                }
            });    
	},
	addFriend: function() {
		var person = GetLS("newperson_detail");
		if(CurrentUser._id == person.from) 
			return (CurrentUser.category==30?$alert("不能添加自己为好友"):$alertEn("You can't add yourself as a friend"));

		var length = CurrentUser.friends && CurrentUser.friends.length || 0;
		for(var i=0; i<length; i++) {
			if(CurrentUser.friends[i].id == person.from)
				return (CurrentUser.category==30?$alert("已经是好友了"):$alertEn("You are the friends alreay"));
		}
        $prompt(CurrentUser.category==30?"提示":"Notice",CurrentUser.category==30?"加他（她）好朋友，请输入你的验证信息":"Add friend, please enter the verification information",CurrentUser.category==30?"我是"+CurrentUser.name:"I am" + CurrentUser.name,CurrentUser.category==30?"确定,取消":"OK,Cancel",function(value){
            /*
            $ajax({
                url: "/user/addFriend",
                data: {
                    user:CurrentUser,
                    friendId: person.from,
                    tip: value
                },
                success: function(res) {
                    SetLS("user",res.result);
                    CurrentUser = res.result;
                    (CurrentUser.category==30?$alert("发送请求成功"):$alertEn("Successfully Sent"));
                }
            });    
            */
           $ajax({
                url: "/message/sdm",
                data: {
                   cid:CurrentUser._id,
                   fid:person.from,
                   cont:value
                },
                success: function(res) {
                    PCall("newperson_detail","WinClose()");
                    (CurrentUser.category==30?$alert("发送请求成功"):$alertEn("Successfully Sent"));
                }
            });
           
           
           
        },""); 
	},
	/*
	selectSheet: function(tab){
			if(CurrentUser.category==30){
			 arr = addressArr;
			}else{
			 arr = addressArr1;   
			}
			ActionSheet(CurrentUser.category==30?"璇烽�鎷╂椿鍔ㄥ湴鐐�:"Please select activity address", CurrentUser.category==30?"鍙栨秷":"Cancel", arr, function(data) {
				$("#" + tab).text(arr[data]);
			});
		},*/
	
	selectSheet: function(tab){
        if(CurrentUser.category==30){
         arr = addressArr;
        }else{
         arr = addressArr1;   
        }
        ActionSheet(CurrentUser.category==30?"请选择活动地点":"Please select activity address", CurrentUser.category==30?"取消":"Cancel", arr, function(data) {
            if(data==4){
            $("#" + tab).attr("data-num",data);
            $("#" + tab).html('<input id="others" type="text" class="ub ub-f1 uc-a1 input_sz value" placeholder="Your City"/>');            
            }else{
            $("#" + tab).attr("data-num",data);
            $("#" + tab).text(arr[data]);
            }
        });
    },
	uploadPhoto: function(){
        Upload.init(Core.API_VER + "/upload", function(res) {
            res = res[0];
            if (res.code) {
                $alert(res.codemsg);
                return;
            }                
            var url = res.result.url; 
            $("#photoList").append('<div data-photo="' + url + '" class="uw70 umauto photo"><img src="' + Core.IMG_VER + url + '" class="uw100"><img src="../img/icon_close.png" style="width:2em;height:2em;z-index:99999;position:absolute;top:-1em;right:-1em;" onclick="NewPerson.closePhoto();"></div>');
        });
    },
    closePhoto: function(){
        var $this = $(event.currentTarget);
        $this.parent().remove();
    },
	addInit: function() {
		$ajax({
			url: "/newperson/list",
			data: {
				from: CurrentUser._id,
				category: BROADCASTTYPE.TYPE_NEWPERSON,
			},
			success: function(res) {
			    var items=res.result.results;
			    SetLS("plist", items);
				if(res.code)
					return $alert(res.codemsg);
		
				if(res.result.results[0]) {
					$(".value").each(function(i){
						$(this).val(res.result.results[0][$(this).attr("id")]);
					});
					$("#aTo").text(res.result.results[0]["aTo"]);
					if(res.result.results[0]["aTo"]==0){
                       $("#aTo").text("BeiJing");
                       $("#aTo").attr("data-num",0);
                    }
					if(res.result.results[0]["aTo"]==1){
                       $("#aTo").text("ShangHai");
                       $("#aTo").attr("data-num",1);
                    }
                    if(res.result.results[0]["aTo"]==2){
                       $("#aTo").text("GuangZhou");
                       $("#aTo").attr("data-num",2);
                    }
                    if(res.result.results[0]["aTo"]==3){
                       $("#aTo").text("ShenZhens");
                       $("#aTo").attr("data-num",3);
                    }
					if(res.result.results[0]["aTo"]==4){
					   $("#aTo").attr("data-num",4);
                       $("#aTo").html('<input id="others" type="text" class="ub ub-f1 uc-a1 input_sz value" placeholder="Your City"/>');            
					   $("#others").val(res.result.results[0]["aToOther"]);
					}
					$.each(res.result.results[0].photo,function(i,item){
						$("#photoList").append('<div res.result.results[0]-photo="' + item + '" class="uw70 umauto photo"><img src="' + Core.IMG_VER + item + '" class="uw100"><img src="../img/icon_close.png" style="width:2em;height:2em;z-index:99999;position:absolute;top:-1em;right:-1em;" onclick="FindJob.closePhoto();"></div>');
					});				
				}
				NewPerson.newperson = res.result.results[0];
			}
		});	
	},
	add: function(){
		var data = NewPerson.newperson;
		if(!data)
			data = {};
		data.from = CurrentUser._id;
		data.category = BROADCASTTYPE.TYPE_NEWPERSON;
		$("body").find(".value").each(function(e){
			data[$(this).attr("id")] = $(this).val();
		});
		data.aTo = $("#aTo").data("num");
		if(data.name == "")
			return (CurrentUser.category==30?$alert("请输入姓名"):$alertEn("Please input your name"));
		if(data.aTo==4){
		    data.aToOther = $("#others").val();
		}
		// if(data.aTo == "")
			// return (CurrentUser.category==30?$alert("请输入你目前的生活地点"):$alertEn("Please check Your Current City"));== "")
			//return (CurrentUser.category==30?$alert("请输入介绍"):$alert("Yourself introduction"));

		data.photo = new Array();
		$("#photoList").find(".photo").each(function(){
			data.photo.push($(this).data("photo"));
		});
		$ajax({
			url: "/newperson/upsert",
			data: data,
			success: function(res){
			    var ml=CurrentUser.category==30?"ch/":"en/";
				if(res.code)
					return $alert(res.codemsg);
				PCall("../"+ml+"newperson", "CCall('NewPerson.InitBeijing();')");
				PCall("../"+ml+"newperson", "CCall('NewPerson.InitShanghai();')");
				PCall("../"+ml+"newperson", "CCall('NewPerson.InitGuangzhou();')");
				PCall("../"+ml+"newperson", "CCall('NewPerson.InitShenzhen();')");
				PCall("../"+ml+"newperson", "CCall('NewPerson.InitOthers();')");
				PCall("newperson_add","WinClose()");
			}
		});
	},	
	remove: function() {
        uexWindow.confirm(CurrentUser.category==30?"警告":"Warning",CurrentUser.category==30?"确定要删除吗？":"Do you want to detete?",CurrentUser.category==30?"确定,取消":"Sure,Cancel");
            function cbConfirm(opId, dataType, ensure){
            if(ensure == 0){
            var newperson = GetLS("plist")[0];
            $ajax({
                url: "/newperson/remove",
                data: {_id: newperson._id},
                success: function(res) {
                    if(res.code)
                        return $alert(res.codemsg);
                    var ml=CurrentUser.category==30?"ch/":"en/";
                    PCall("../"+ml+"newperson", "CCall('NewPerson.InitBeijing();')");
                    PCall("../"+ml+"newperson", "CCall('NewPerson.InitShanghai();')");
                    PCall("../"+ml+"newperson", "CCall('NewPerson.InitGuangzhou();')");
                    PCall("../"+ml+"newperson", "CCall('NewPerson.InitShenzhen();')");
                    PCall("../"+ml+"newperson", "CCall('NewPerson.InitOthers();')");
                    PCall("newperson_add","WinClose()");
                }
            });
          }
        }
        uexWindow.cbConfirm = cbConfirm; 
    },
}